<?php

namespace App\Http\Requests\VisitWebTask;

use Illuminate\Foundation\Http\FormRequest;

class Show extends FormRequest
{
    public function rules()
    {
        return [
            'id' => 'bail|required',
            'time' => 'bail|required|date|after:-10 minutes',
            'agent_code' => 'bail|required',
            'sign' => 'bail|required',
        ];
    }


    /**
     * 配置验证器实例。
     *
     * @param \Illuminate\Validation\Validator $validator
     * @return void
     */
    public function withValidator($validator)
    {
        if (!$validator->validate()){
            return;
        }
        $validator->after(function ($validator) {
            $validated = $validator->validated();
            if (!Create::getAgent($validated['agent_code'])) {
                $validator->errors()->add('agent_code', 'agent不存在');
                return false;
            }
            if ($validated['sign'] != Create::sign($validated,Create::getAgent($validated['agent_code']))) {
                $validator->errors()->add('sign', '签名错误');
                return false;
            }
        });
    }
}
